package mine;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JWindow;
import javax.swing.border.LineBorder;

public class Welcomewin extends JWindow {

	private static final long serialVersionUID = -2970626103134592561L;

	private Controller controller;
	private JLabel jLabel;
	private static final String[] strings = { "载入中", "载入中.", "载入中..", "载入中..." };
	private boolean prepared;

	public Welcomewin(Controller controller) {
		this.setController(controller);
		init();
	}

	private void init() {
		JLayeredPane pane = new JLayeredPane();
		ImageIcon ico = new ImageIcon(
				Welcomewin.class.getResource("saoleiwin.png"));
		setSize(ico.getIconWidth(), ico.getIconHeight());
		JLabel l = new JLabel(ico);
		l.setBounds(0, 0, ico.getIconWidth(), ico.getIconHeight());
		pane.add(l, JLayeredPane.DEFAULT_LAYER);
		jLabel = new JLabel("载入中");
		jLabel.setFont(new Font(Font.MONOSPACED, Font.TYPE1_FONT, 23));
		jLabel.setForeground(new Color(160, 120, 250));
		jLabel.setBounds(270, 30, 200, 30);
		pane.add(jLabel, JLayeredPane.POPUP_LAYER);
		pane.setBorder(new LineBorder(Color.GRAY));
		setContentPane(pane);
		Toolkit toolkit = Toolkit.getDefaultToolkit();
		Dimension dimension = toolkit.getScreenSize();
		setLocation((dimension.width - getWidth()) / 2,
				(dimension.height - getHeight()) / 2);
	}

	public void toShow() {
		setVisible(true);
		new Thread() {
			public void run() {
				try {
					sleep(1500);
					prepared = false;
					while (!prepared) {
						sleep(500);
						if (controller != null) {
							prepared = true;
							dispose();
							sleep(500);
							controller.start();

						}
					}

				} catch (InterruptedException e) {
					e.printStackTrace();
				}

			};
		}.start();

		new Thread() {
			public void run() {
				int i = 0;
				try {
					prepared = false;
					while (!prepared) {
						jLabel.setText(strings[(i++) % 4]);
						sleep(250);
					}
				} catch (InterruptedException e) {
					e.printStackTrace();
				}

			};
		}.start();
	}

	public void setController(Controller controller) {
		this.controller = controller;
	}

	public Controller getController() {
		return controller;
	}

	public static void main(String[] args) {
		MineModel model = new MineModel(12, 12);
		Controller controller = new Controller(model, 12, 12);
		Welcomewin win = new Welcomewin(controller);
		win.toShow();
	}

}